﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using BuildMonitor.Engine;
using Microsoft.TeamFoundation.Build.Client;

namespace BuildMonitor
{
	public partial class MainWindow
	{

		public ConsoleText consoleText = new ConsoleText();
		public MainWindow()
		{
			InitializeComponent();
            ObjectDataProvider consoleData = (ObjectDataProvider)FindResource("consoleData");
			consoleData.ObjectInstance = consoleText;
		}

		private bool MinimizeTipShownOnce;
		private WindowState storedWindowState = WindowState.Normal;
		private void Window_StateChanged(object sender, EventArgs e)
		{
			if (WindowState == WindowState.Minimized)
			{
				ShowInTaskbar = false;
				if (notifyIcon != null && !MinimizeTipShownOnce)
				{
					ShowBalloonTip("The TFS Build Monitor is still running. Click here to open the window again.");
					MinimizeTipShownOnce = true;
				}
			}
			else
			{
				storedWindowState = WindowState;
				ShowInTaskbar = true;
			}
		}

		private void notifyIcon_MouseClick(object sender, MouseButtonEventArgs e)
		{
			if (e.ChangedButton == MouseButton.Left)
			{
				WindowState = storedWindowState;
			}
		}

		private BuildWatcher watcher;
		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			watcher = new BuildWatcher(consoleText);
			PublisherFactory.InstantiatePublishers(consoleText, watcher);
			watcher.BuildCompletionEvent += watcher_BuildCompletionEvent;
			watcher.BuildQualityChangeEvent += watcher_BuildQualityChangeEvent;
			watcher.Start();
			consoleText.WriteLine("Build Monitor started");
		}

		void watcher_BuildQualityChangeEvent(object sender, BuildStoreEventArgs buildWatcherEventArgs)
		{
			ShowBalloonTip(String.Format("{0} changed the build quality to {1}", buildWatcherEventArgs.Data.LastChangedBy, buildWatcherEventArgs.Data.Quality));
		}

		private void ShowBalloonTip(string text)
		{
			if (System.Threading.Thread.CurrentThread != notifyIcon.Dispatcher.Thread)
			{
				notifyIcon.Dispatcher.Invoke(DispatcherPriority.DataBind, new Action(() =>
					{
						notifyIcon.BalloonTipText = text;
						notifyIcon.ShowBalloonTip(2000);
					}));
			}
			else
			{
				notifyIcon.BalloonTipText = text;
				notifyIcon.ShowBalloonTip(2000);
			}
		}

		void watcher_BuildCompletionEvent(object sender, BuildStoreEventArgs buildWatcherEventArgs)
		{
			var status = buildWatcherEventArgs.Data.Status;
			switch (status)
			{
				case BuildStatus.Succeeded:
					ShowBalloonTip("Build Succeeded");
					break;
				case BuildStatus.Failed:
					ShowBalloonTip("Build Failed");
					break;
				case BuildStatus.Stopped:
					ShowBalloonTip("Build Stopped");
					break;
				case BuildStatus.InProgress:
					ShowBalloonTip("Build Started");
					break;
			}
		}

		private void Window_Closing(object sender, CancelEventArgs e)
		{
			watcher.BuildCompletionEvent -= watcher_BuildCompletionEvent;
			watcher.BuildQualityChangeEvent -= watcher_BuildQualityChangeEvent;
			watcher.Stop();
		}
	}
}
